//
//  ZXStarAppraiseDisplayView.m
//  ZXStarAppraiseViewDemo
//
//  Created by LIUKUN-PC on 16/5/12.
//  Copyright © 2016年 liukun. All rights reserved.
//

#import "ZXStarAppraiseDisplayView.h"

const CGFloat ZXStarAppraiseDisplayViewSuggestWidth = 320.0;
const CGFloat ZXStarAppraiseDisplayViewSuggestHeight = 158.0;

@interface ZXStarAppraiseDisplayView ()

@property (nonatomic, strong) UIView *totalScoreView; //总概述
@property (nonatomic, strong) UIView *levelStarScoreView; //各个等级

@property (nonatomic, strong) UILabel *scoreLabel; //总评分
@property (nonatomic, strong) UILabel *totalCountLabel; //评论总数

@property (nonatomic, strong) NSArray<UILabel *> *scoreLableArray;
@property (nonatomic, strong) NSArray<UIImageView *> *starImageViewArray;

/**
 *  评论总数
 */
@property (nonatomic, assign) NSInteger totalCount;
/**
 *  结果
 */
@property (nonatomic, assign) double score;

@end

@implementation ZXStarAppraiseDisplayView

#pragma mark - API

- (void)refreshViewFrame {
    [self.superview layoutIfNeeded];
    
}

#pragma mark - OverWrite Init Methods

- (instancetype)init {
    if (self = [super init]) {
        [self initSubViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initSubViews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];
    }
    return self;
}

#pragma mark - initSubViews

- (void)initSubViews {
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:self.totalScoreView];
    [self addSubview:self.levelStarScoreView];
}

#pragma mark - Getter & Setter

- (UIView *)totalScoreView {
    if (_totalScoreView) {
        return _totalScoreView;
    }
    
    NSMutableArray *imageViewMutableArray = [NSMutableArray array];
    
    _totalScoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 33)];
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *starImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3 + i * 33, 3, 27, 27)];
        starImageView.image = [UIImage imageNamed:@"star_empty_new"];
        [imageViewMutableArray addObject:starImageView];
        
        [_totalScoreView addSubview:starImageView];
    }
    self.scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 0, 30, 33)];
    self.scoreLabel.textAlignment = NSTextAlignmentCenter;
    self.scoreLabel.textColor = [UIColor orangeColor];
    self.scoreLabel.font = [UIFont systemFontOfSize:15];
    [_totalScoreView addSubview:self.scoreLabel];
    
    self.totalCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.scoreLabel.frame), 0, _totalScoreView.frame.size.width - CGRectGetMaxX(self.scoreLabel.frame), 33)];
    self.totalCountLabel.font = [UIFont systemFontOfSize:15];
    [_totalScoreView addSubview:self.totalCountLabel];
    
    self.starImageViewArray = [imageViewMutableArray copy];
    return _totalScoreView;
}

- (UIView *)levelStarScoreView {
    if (_levelStarScoreView) {
        return _levelStarScoreView;
    }
    _levelStarScoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 33, 250, 150)];
    
    NSMutableArray *scoreLabelMutablArray = [NSMutableArray array];
    
    for (NSInteger row = 0; row < 5; row++) {
        for (NSInteger column = 0; column < 5; column++) {
            UIImageView *starImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2.5 + column * 25, 2.5 + row * 25, 20, 20)];
            if (column <= 4 - row) {
                starImageView.image = [UIImage imageNamed:@"star_full_new"];
            } else {
                starImageView.image = [UIImage imageNamed:@"star_empty_new"];
            }
            [_levelStarScoreView addSubview:starImageView];
        }
        UILabel *levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, row * 25, 40, 30)];
        levelLabel.text = [NSString stringWithFormat:@"%ld分", 5 - row];
        levelLabel.textAlignment = NSTextAlignmentRight;
        levelLabel.font = [UIFont systemFontOfSize:15];
        [_levelStarScoreView addSubview:levelLabel];
        
        UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(levelLabel.frame), row * 25, 50, 30)];
        scoreLabel.textAlignment = NSTextAlignmentRight;
        scoreLabel.font = [UIFont systemFontOfSize:15];
        scoreLabel.textColor = [UIColor orangeColor];
        
        [scoreLabelMutablArray addObject:scoreLabel];
        [_levelStarScoreView addSubview:scoreLabel];
     }
    self.scoreLableArray = [scoreLabelMutablArray copy];
    return _levelStarScoreView;
}

- (void)setLevelStarCount:(NSArray<NSNumber *> *)levelStarCount {
    
    NSAssert(levelStarCount.count >= 5, @"星级评价数组元素不能少于5");
    
    _levelStarCount = levelStarCount;
    self.totalCount = 0;
    self.score = 0;
    for (NSInteger i = 0; i < 5; i++) {
        self.totalCount += _levelStarCount[i].integerValue;
        self.score += _levelStarCount[i].integerValue * (5-i);
    }
    if (self.totalCount == 0) {
        self.score = 0;
    } else {
        self.score /= self.totalCount;
    }
    //去除边界值
    if (self.score > 5) {
        self.score = 5;
    } else if (self.score < 0){
        self.score = 0;
    }
    
    //double精确性
    for (int i = 0; i < 6; i++) {
        if (ABS(self.score - i) < 0.000001) {
            self.score = i;
            break;
        }
    }
    
    NSString *numberStr = [NSString stringWithFormat:@"%ld", self.totalCount];
    if (self.totalCount >= 10000 && self.totalCount < 100000000) {
        numberStr = [NSString stringWithFormat:@"%.1lf万", self.totalCount / 10000.0];
    } else if (self.totalCount >= 100000000) {
        numberStr = [NSString stringWithFormat:@"%.1lf亿", self.totalCount / 100000000.0];
    }
   
    
    self.totalCountLabel.text = [NSString stringWithFormat:@"分 共%@份评分", numberStr];
    
    self.scoreLabel.text = [NSString stringWithFormat:@"%.1lf", self.score];
    
    int floorScore = floor(self.score);
    int ceilScore = ceil(self.score);
    
    for (NSInteger i = 0; i < 5; i++) {
        self.scoreLableArray[i].text = [NSString stringWithFormat:@"%.1f%%", self.totalCount ? (self.levelStarCount[i].integerValue * 100.0 / self.totalCount): 0];
        if (i + 1 <= floorScore) {
            self.starImageViewArray[i].image = [UIImage imageNamed:@"star_full_new"];
        } else if (i + 1 <= ceilScore && i + 1 > floorScore) {
            self.starImageViewArray[i].image = [UIImage imageNamed:@"star_half_new"];
        } else if (i + 1 > ceilScore) {
            self.starImageViewArray[i].image = [UIImage imageNamed:@"star_empty_new"];
        }
    }
}





@end
